package com.flame.workflow.ru.service;

import com.flame.core.id.IdWorker;
import com.flame.core.utils.DateUtils;
import com.flame.workflow.constants.UserTaskConstants;
import com.flame.workflow.ru.controller.vo.UserTaskDetailVo;
import com.flame.workflow.ru.controller.vo.UserTaskVo;
import com.flame.workflow.ru.entity.UserTask;
import com.flame.workflow.ru.mapper.TaskCommentMapper;
import com.flame.workflow.ru.mapper.UserTaskMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * flame_workflow_ru_user_task 流程实例任务用户评论表
 *
 * @author 吴欣童
 * @since 2023-07-04 14:12:32
 */
@Service
public class UserTaskService {

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private UserTaskMapper userTaskMapper;

    @Autowired
    private TaskCommentMapper taskCommentMapper;

    public List<UserTaskDetailVo> pageUserTask(UserTaskVo userTaskVo) {
        return userTaskMapper.page(userTaskVo);
    }

    public void completeByTaskIdAndUserId(Long taskId, String userId, String userTaskStatus,
                                          boolean deleteOtherUserTaskFlag) {
        if (deleteOtherUserTaskFlag) {
            List<UserTask> userTaskList = userTaskMapper.listByTaskId(taskId);
            for (UserTask userTask : userTaskList) {
                if (userId.equals(userTask.getUserId())) {
                    continue;
                }
                userTaskMapper.deleteByTaskIdAndUserId(userTask.getTaskId(), userTask.getUserId());
                taskCommentMapper.deleteByTaskIdAndUserId(userTask.getTaskId(), userTask.getUserId());
            }
        }
        UserTask userTask = new UserTask();
        userTask.setTaskId(taskId);
        userTask.setUserId(userId);
        userTask.setUserTaskStatus(userTaskStatus);
        userTask.setEndTime(DateUtils.currentDateTime());
        userTaskMapper.updateByTaskIdAndUserId(userTask);
    }

    public void addUserTask(Long instanceId, Long taskId, String userId) {
        UserTask dbUserTask = userTaskMapper.findByTaskIdAndUserId(taskId, userId);
        if (!ObjectUtils.isEmpty(dbUserTask)) {
            return;
        }
        UserTask userTask = new UserTask();
        userTask.setUserTaskId(idWorker.nextId());
        userTask.setInstanceId(instanceId);
        userTask.setTaskId(taskId);
        userTask.setUserId(userId);
        userTask.setUserTaskStatus(UserTaskConstants.USER_TASK_STATUS_CREATE);
        userTask.setCreateTime(DateUtils.currentDateTime());
        userTaskMapper.insert(userTask);
    }

    public List<UserTask> listCreateUserTask(Long taskId) {
        UserTask userTask = new UserTask();
        userTask.setTaskId(taskId);
        userTask.setUserTaskStatus(UserTaskConstants.USER_TASK_STATUS_CREATE);
        return userTaskMapper.list(userTask);
    }

    public UserTask getByTaskIdAndUserId(Long taskId, String userId) {
        return userTaskMapper.findByTaskIdAndUserId(taskId, userId);
    }

    public List<UserTask> listByInstanceId(Long instanceId) {
        return userTaskMapper.listByInstanceId(instanceId);
    }

    public void updateUserTask(UserTask userTask) {
        userTaskMapper.updateById(userTask);
    }

    public List<UserTask> listByTaskId(Long taskId) {
        return userTaskMapper.listByTaskId(taskId);
    }

    public void deleteUserTask(Long userTaskId) {
        userTaskMapper.deleteById(userTaskId);
    }
}
